package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.CompanyMapper;
import com.wanmait.recruitment.pojo.Company;
import com.wanmait.recruitment.pojo.Job;
import com.wanmait.recruitment.service.CompanyService;
import com.wanmait.recruitment.service.MailService;
import com.wanmait.recruitment.util.IDUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class CompanyServicelmpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private MailService mailService;
    @Override
    public void insert(Company object) {

    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void delete(Integer id) {
         companyMapper.delete(id);

    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void delete(String ids) {
        companyMapper.batchDelete(ids);

    }
    @Override
    public  Company FindAllCompany(Integer id){
        return  companyMapper.FinAallCompany(id);

    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void updateById(Company company) {
        companyMapper.updateById(company);
    }

    @Override
    public Company findById(Integer id) {
        return companyMapper.findById(id);
    }

    @Override
    @Cacheable("companyCache")
    public List<Company> findAll() {
        return companyMapper.select();
    }

    //注册公司用户
    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public  void register(Company company){
        companyMapper.register(company);
//        //获取激活码
//        String code= company.getActiveCode();
//        //主题
//        String subject = "壮壮招聘网的激活邮件";
//        //上面的激活码发送到用户注册邮箱
//        String context = "<a href=\"http://localhost:8080/zhuang/checkCodeCompany?code="+code+"\">激活请点击:"+code+"</a>";
//        //发送激活邮件
//        mailService.sendMimeMail (company.getEmail(),subject,context);

    }

    @Override
    public void update(Company company) {


    }


    @Override
    public Company getUserByActiveCode(String activeCode) {
        return companyMapper.getUserByActiveCode(activeCode);
    }
    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void modify(Company company) {
        companyMapper.update(company);
    }

    //验证用户名是否存在
    @Override
    public String checkUsername(String username) {

        return companyMapper.checkUsername(username);
    }

    @Override
    @Cacheable("companyCache")
    public List<Company> companySearchJudge(String searchInput) {
        return companyMapper.companySearchJudge(searchInput);

    }

    @Override
    public Company companySearch(String searchInput) {
        return companyMapper.selectBycompanyName(searchInput);
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public int insertNowJobCount(Integer id) {
        return companyMapper.insertNowJobCount(id);
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public int insertJobCount(Integer id) {
        return companyMapper.insertJobCount(id);
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public int updateNowJobCount() {
        return companyMapper.updateNowJobCount();
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void updateResumeCount(Integer id,Integer change) {
        companyMapper.updateResumeCount(id,change);
    }

    @Override
    public Company selectFindByCompanyById(Integer id) {
        return companyMapper.selectFindByCompanyById(id);
    }



    @Override
    public PageInfo<Company> findPageAll(int pageNum, int pageSize, Integer cityId, Integer stageId, Integer industryId, String companyName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Company>companyList=companyMapper.selectPage(cityId,stageId,industryId,companyName);
        PageInfo<Company>pageInfo=new PageInfo<>(companyList);

        return pageInfo;
    }

    public Company FindById(Integer id) {
        return companyMapper.selectFindByCompanyById(id);
    }

    @Override
    public Company findByIdAndEmail(String email,Integer id) {
        return companyMapper.selectByIdAndEmail(email,id);
    }


    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void openCreate(String email) {
        //获取激活码
        String code= IDUtils.getUUID();
        //主题
        String subject = "壮壮招聘网:开通招聘服务";
        //上面的激活码发送到用户注册邮箱
        String context = "<a href=\"http://localhost:8080/zhuang/company/openSuccess\">激活请点击:"+code+"</a>";
        //发送激活邮件
        mailService.sendMimeMail (email,subject,context);
    }

    @Override
    @Cacheable("companyCache")
    public List<Company> findByName(String name) {
        return companyMapper.findByName(name);
    }


    @Override
    public Company login(String email, String userpass) {
        return companyMapper.login(email, userpass);
    }

    @Override
    public Company findByUsername(String email) {
        return companyMapper.findByUsername(email);
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void updateUserPass(String email, String userpass) {
         companyMapper.updateUserpass(email, userpass);
    }


    @Override
    public List<Company> findAll(Company object) {
        return null;
    }

    @Override
    @Cacheable("companyCache")
    public PageInfo<Company> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Company>companyList=companyMapper.select();
        PageInfo<Company>pageInfo=new PageInfo<>(companyList);
        return pageInfo;
    }
    @Override
    @Cacheable("companyCache")
    public  PageInfo<Company>findPageManage(Integer pageNum, Integer pageSize,Integer cityId,Integer industryId,Integer stageId,String companyName){
        PageHelper.startPage(pageNum,pageSize);
        List<Company>companyList=companyMapper.selectFindAll(cityId,stageId,industryId,companyName);

        PageInfo<Company>pageInfo=new PageInfo<>(companyList);
        return pageInfo;

    }
    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public  void upudateAuthable(Integer id){
        companyMapper.updateauthable(id);
    }
    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public  void upudateOpened(Integer id){
        companyMapper.updateOpened(id);
    }

    @Override
    @CacheEvict(value = "companyCache",allEntries = true)
    public void updatePassword(String userpass, Integer id) {
        companyMapper.updatepassword(userpass,id);
    }


}
